<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="s-header">
        <div>
          <p>区县市下发：</p>
          <div style="margin: 8px 0;">
            <a-tag closable @close="deleteUser(item)" v-for="(item, i) in selectedRow" :key="i">{{ item.depart }}</a-tag>
          </div>
        </div>
        <a-button type="primary" @click="showModal=true">选择对象</a-button>
      </div>
      <div>
        <a-textarea v-model="form.askContent" placeholder="请输入内容" :autosize="{ minRows: 5, maxRows: 8 }"></a-textarea>
      </div>
      <div style="text-align: center;margin-top: 20px;">
        <a-button type="primary" style="width: 120px" @click="submit">提 交</a-button>
      </div>
    </a-card>


    <a-modal v-model="showModal" width="700px">
      
      <div class="line-title-head"><span>接受对象</span></div>
      <div style="margin: 8px 0;">
        <a-space>
          <a-select allowClear style="width: 160px" v-model="params.isOut" placeholder="内外协" @change="change">
            <a-select-option value="0">区县市网信办</a-select-option>
            <a-select-option value="1">市级部门</a-select-option>
          </a-select>
          <a-input allowClear style="width: 160px" v-model="params.depart" placeholder="请输入单位名称"></a-input>
          <a-button type="primary" @click="getList">筛选</a-button>
        </a-space>
      </div>
      
      <h-table
        ref="htable"
        @init="getList"
        :config="config"
        showRowSelection
        :showHeadBgNone="false"
        :zebraStripes="false"
        :dataAll="dataAll"
        @select="select">
        <span slot="action" slot-scope="{record}"><a style="color: #1F3B8A" @click="editScore(record)">编辑</a></span>
        <span slot="isOut" slot-scope="{record}">
          {{ record.isOut == 1 ? '市级部门' : '区县市网信办' }}
        </span>
      </h-table>

      <template slot="footer">
        <a-button key="back" type="primary" @click="showModal = false">
          确 定
        </a-button>
      </template>
    </a-modal>
  </page-header-wrapper>
  
</template>
<script>
import moment from 'moment';
import { createEvent, address } from '@/api/serve';
import { getIsOutDepart } from '@/api/dept';
import cloneDeep from 'lodash/cloneDeep';

export default {
  watch: {
    
  },
  data() {
    return {
      moment,
      form: {
        
      },
      showModal: false,
      params: {
        address: undefined,
        depart: ''
      },
      address: [],
      departId: [],
      config: {
        rowKey: 'id',
        bordered: true,
        dataSource: [],
        scroll: {
          y: 250,
          x: false
        },
        columns: [
          // {
          //   align: 'center',
          //   title: '地址',
          //   dataIndex: 'address'
          // },
          // {
          //   align: 'center',
          //   title: '内外协',
          //   dataIndex: 'isOut',
          //   // width: 300,
          //   scopedSlots: { customRender: 'isOut' }

          // },
          {
            align: 'center',
            title: '单位名称',
            dataIndex: 'depart'
            // width: 300
          },
          {
            align: 'center',
            title: '接收人',
            dataIndex: 'realname'
            // width: 300
          }
        ],
        pagination: false
      },
      selectedRow: [],
      dataAll: [],
      options: [
        { label: '区县市网信办', value: 1 },
        { label: '市级部门', value: 2 }
      ]
    };
  },
  created() {
    this.getList(true);
    this.getAddress();
  },
  methods: {
    submit() {
      if (!this.departId.length) {
        this.$message.error('请选择单位');
        return;
      }
      if (!this.form.askContent) {
        this.$message.error('请输入内容');
        return;
      }
      createEvent({ ...this.form, departIdList: this.departId, handleType: 0 }).then(res => {
        this.$message.success('下发成功');

        this.departId = [];
        this.selectedRow = []
        this.form = {}
        this.$refs.htable.cancel()
      });
    },
    getAddress() {
      address().then(res => {
        this.address = res.data;
      });
    },
    select({ selectedRowKeys, selectedRow }) {
      this.departId = selectedRowKeys;
      this.selectedRow = this.dataAll.filter(e => selectedRowKeys.includes(e.id));
    },
    change(e) {
      // alert(JSON.stringify(e));
      if (e == 0) {
        this.config.columns = [
          {
            align: 'center',
            title: '单位名称',
            dataIndex: 'depart'
            // width: 300
          },
          {
            align: 'center',
            title: '接收人',
            dataIndex: 'realname'
            // width: 300
          }
        ];
      } else if (e == 1) {
        this.config.columns = [
          {
            align: 'center',
            title: '单位名称',
            dataIndex: 'depart'
            // width: 300
          },
          {
            align: 'center',
            title: '接收人',
            dataIndex: 'realname'
            // width: 300
          }
        ];
      } else {
        this.config.columns = [
          {
            align: 'center',
            title: '地址',
            dataIndex: 'address'
          },
          {
            align: 'center',
            title: '内外协',
            dataIndex: 'isOut',
            // width: 300,
            scopedSlots: { customRender: 'isOut' }

          },
          {
            align: 'center',
            title: '单位名称',
            dataIndex: 'depart'
            // width: 300
          }
        ];
      }
    },
    getList(showAll) {
      getIsOutDepart({
        // isOut: this.deptType,
        eventId: this.id,
        ...this.params
      }).then(res => {
        this.config.dataSource = res.data;
        if (showAll === true) {
          this.dataAll = res.data;
        }
      });
    },
    deleteUser({ id }) {
      const indexs = this.selectedRow.findIndex(e => e.id == id);
      console.log('[indexs]', indexs);
      if (indexs > -1) {
        this.selectedRow.splice(indexs, 1);
        
        this.departId.splice(indexs, 1);
        console.log('[update departId]', this.departId);
        // this.$refs.htable.selectedRowKeys.splice(indexs, 1);
        this.$refs.htable.update(this.departId);
      }
      // const index = this.form.userIdList.findIndex(e => e == id);
      // // const indexRow = this.form.userIdList.findIndex(e => e.id == id);
      // if (index > -1) this.form.userIdList.splice(index, 1);
    },
  }
};
</script>
<style lang="less" scoped>
.s-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
}
</style>